/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.db_vdumper.util;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *  Delegator for db-manager.
 * @author yura
 */
public class QueryAdapter implements QueryHandler{
    public static final int CONCUR = ResultSet.CONCUR_UPDATABLE;
    public static final int INSERT = ResultSet.TYPE_SCROLL_INSENSITIVE;
    private DbManager dbManager;
    private SQLException exception = null;

    public QueryAdapter(DbManager dbManager) {
        this.dbManager = dbManager;
    }

    @Override
    public boolean sendQuery(String query) {
        exception = null;
        try {
            dbManager.clear();

            Statement st = dbManager.getConnection().createStatement(INSERT, CONCUR);
            dbManager.setStatement(st);

            ResultSet result = st.executeQuery(query);
            dbManager.setResultSet(result);
            dbManager.setMetaDataFromQuery();

            return true;
        } catch (SQLException ex) {
            Logger.getInstance().error(this, "Can't send query \"" + query + "\"");
//            Logger.getInstance().error(this, ex);
            exception = ex;
            return false;
        }
    }

    @Override
    public boolean sendQueryUpdate(String query) {
        try {
            dbManager.clear();
            
            Statement st = dbManager.getConnection().createStatement(INSERT, CONCUR);
            dbManager.setStatement(st);
            st.executeUpdate(query);

            return true;
        } catch (SQLException ex) {
            Logger.getInstance().error(this, "Can't send update query \"" + query + "\"");
            Logger.getInstance().error(this, ex);
            return false;
        }
    }

    @Override
    public boolean sendQueryGetTables(String catalog, String schema, String name) {
        try {
            dbManager.clear();

            DatabaseMetaData data = dbManager.getConnection().getMetaData();
            ResultSet result = data.getTables(catalog, schema, name, new String[]{"TABLE"});

            dbManager.setResultSet(result);
            dbManager.setMetaDataFromQuery();

            return true;
        } catch (SQLException ex) {
            Logger.getInstance().error(this, "Can't get table list");
            Logger.getInstance().error(this, ex);
            return false;
        }
    }

    @Override
    public boolean sendQueryGetAllTables() {
        return sendQueryGetTables(null, null, "%");
    }

    @Override
    public boolean sendQueryGetColumns(String catalog, String schema, String table) {
        try {
            dbManager.clear();

            DatabaseMetaData data = dbManager.getConnection().getMetaData();
            ResultSet result = data.getColumns(catalog, schema, table, "%");

            dbManager.setResultSet(result);
            dbManager.setMetaDataFromQuery();

            return true;
        } catch (SQLException ex) {
            String tn = table != null? "\"" + table + "\"": "all";
            Logger.getInstance().error(this, "Can't get column list for " + tn);
            Logger.getInstance().error(this, ex);
            return false;
        }
    }

    @Override
    public boolean sendQueryGetAllColumns() {
        return sendQueryGetColumns(null, null, "%");
    }

    @Override
    public boolean sendQueryGetForeignKeys(String catalog, String schema, String table) {
        try {
            dbManager.clear();

            DatabaseMetaData data = dbManager.getConnection().getMetaData();
            ResultSet result = data.getExportedKeys(catalog, schema, table);

            dbManager.setResultSet(result);
            dbManager.setMetaDataFromQuery();

            return true;
        } catch (SQLException ex) {
            String tn = table != null? "\"" + table + "\"": "all";
            Logger.getInstance().error(this, "Can't get foreign keys list for " + tn);
            Logger.getInstance().error(this, ex);
            return false;
        }
    }

    @Override
    public boolean sendQueryGetAllForeignKeys() {
        return sendQueryGetForeignKeys(null, null, "%");
    }

    @Override
    public SQLException getException() {
        return exception;
    }

}
